package com.guanjia.web;

import com.guanjia.entity.CreditInformation;
import com.guanjia.entity.User;
import com.guanjia.entity.UserInformation;
import com.guanjia.entity.vo.BorrowInfoVo;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.BorrowService;
import com.guanjia.service.impl.BorrowServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

/**
 * 借贷
 */
@Controller
@RequestMapping("/borrowCenter")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/index")
    public String borrowList(){return "cfgj/jiedai_list";}

    /**
     * 借贷第一个界面
     * @return
     */
    @RequestMapping("/from")
    public String borrowFrom(HttpSession session,Model model){
        User user=(User)session.getAttribute("user");
        if(user==null){
            return "redirect:/login";
        }
        UserInformation userInformation=userMapper.findUserInfoByUserId(user.getId());
        if(userInformation.getName()==null||userInformation.getName().equals("")
             ||userInformation.getIDcard().equals("")   ){
            return "redirect:/userCenter/safe";
        }
        if(userInformation.getNowaddress()==null
                ||userInformation.getIDcard().equals("")   ){
            return "redirect:/userCenter/safe";
        }
        model.addAttribute("userInf",userInformation);

        return "cfgj/jiedai_from";
    }


    /**
     * 借贷第二个界面
     * @return
     */
    @RequestMapping("/from2")
    public String borrowFrom2(){
        return "cfgj/jiedai_from2";
    }

    /**
     * 借贷第三个界面
     * @return
     */
    @RequestMapping("/from3")
    public String borrowFrom3(){
        return "cfgj/jiedai_from3";
    }

    /**
     * 借贷第最后一个界面
     */
    @RequestMapping("/from4")
    public String borrowFrom4(){
        return "cfgj/jiedai_from4";
    }


    /**
     * borrowInfoVo  获取借贷页面传递的参数 重新包装
     * request  获取request域存放生成的借贷项目的id
     * @param borrowInfoVo
     * @param request
     * @return
     */
    @RequestMapping("first")
    public String borrowFirst(BorrowInfoVo borrowInfoVo, HttpServletRequest request){
        HttpSession session=request.getSession();

       String result= borrowService.firstBorrow(borrowInfoVo);
       session.setAttribute("credit_id",result);
        return "redirect:/borrowCenter/from2";
    }

    //下载文件
    @RequestMapping("/downloadOnlineLearnMaterials")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "p2pLoanagreement.pdf";// 设置文件名，根据业务需要替换成要下载的文件名
        if (fileName != null) {
            //设置文件路径
            String realPath = "F://picall//cfgj";
            File file = new File(realPath , fileName);//设置文件路径和名字
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

}
